'use strict';

Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.validate = void 0;
var trasver = function trasver(rows, columns, _ref) {
  var validateRow = _ref.validateRow,
    level = _ref.level;
  var columnsWithRequired = columns.filter(function (column) {
    return column.required;
  });
  var fieldNamesShouldRequired = columnsWithRequired.map(function (column) {
    return column.key;
  });
  rows.forEach(function (row) {
    // 必填校验
    fieldNamesShouldRequired.forEach(function (name) {
      if (row[name] === undefined || row[name] === null || row[name] === '') {
        var column = columnsWithRequired.find(function (column) {
          return column.key === name;
        });
        // 继承父级不必校验必填
        if (column.inherit && level !== 1) return;
        throw new Error(''.concat(column.title, '\u4E0D\u80FD\u4E3A\u7A7A'));
      }
    });
    if (validateRow) validateRow(row);
    // 子级校验
    if (row.children) {
      trasver(row.children, columns, {
        validateRow: validateRow,
        level: level + 1,
      });
    }
  });
  // 重复校验
  var columnsWithUniqued = columns.filter(function (column) {
    return column.uniqued;
  });
  var fieldNamesShouldUnique = columnsWithUniqued.map(function (column) {
    return column.key;
  });
  fieldNamesShouldUnique.forEach(function (name) {
    var vals = rows.map(function (row) {
      return row[name];
    });
    var setVals = new Set(vals);
    if (setVals.size < vals.length) {
      var column = columnsWithUniqued.find(function (column) {
        return column.key === name;
      });
      throw new Error(
        '\u540C\u4E00\u5C42\u7EA7\u4E0D\u80FD\u6709\u76F8\u540C\u7684'.concat(column.title),
      );
    }
  });
};
/** 校验 */
var validate = function validate(values, columns, validateRow) {
  trasver(values, columns, {
    validateRow: validateRow,
    level: 1,
  });
};
exports.validate = validate;
